package com.example.zhaicaoapplicationv3.Book;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.example.zhaicaoapplicationv3.TextEdit_Activity;
import com.example.zhaicaoapplicationv3.MyGridViewPackage.MyGridViewAdapter;
import com.example.zhaicaoapplicationv3.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.HashMap;
/**
 * 无底部导航栏的UI设计、菜单显示且监听功能、创建功能
 * 显示摘抄集中的每一条文本或图片摘抄,整体的功能与Main_Activity相似
 * @author lxx
 * 2019.10.19
 */
public class Book_Activity extends AppCompatActivity implements AdapterView.OnItemLongClickListener{
    private GridView mGridView;
    private MyGridViewAdapter mAdapter;
    private boolean isShowDelete = false;
    private ArrayList<HashMap<String, Object>> myList;
    /**
     * 测试用例，利用字符串和图片数组来测试主页面UI设计
     * GridView来动态生成所需要的显示信息，包括两个String类型数据和一个image类型数据
     * 其实时间信息String使用静态数据测试，便于测试UI
     *  * @author lxx
     *  * 2019.10.19
     */
    final String[] name = new String[]{
            "【文本摘抄】摘抄一", "【文本摘抄】摘抄二","【文本摘抄】摘抄三", "【文本摘抄】摘抄四",
            "【图片摘抄】摘抄一", "【图片摘抄】摘抄二","【图片摘抄】摘抄三", "【图片摘抄】摘抄四",};
    final int[] pic = new int[]{
            R.drawable.zhaicao,R.drawable.zhaicao,
            R.drawable.zhaicao,R.drawable.zhaicao,
            R.drawable.zhaicao,R.drawable.zhaicao,
            R.drawable.zhaicao,R.drawable.zhaicao,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);

        //实现返回arrow(<-)图标:判断父Activity是否为空,不为空设置导航图标
        if(NavUtils.getParentActivityName(Book_Activity.this) != null){
            //显示向左箭头图标
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        /**
         * //监控页面右下角加号(+)号按钮的单击行为，调用自定义函数Plusbutton_3来显示创建窗口
         * @author lxx
         * 2019.10.19
         */
        //监控加(+)号按钮的单击事件行为并作出处理
        FloatingActionButton bt_plus1 = findViewById(R.id.bt_puls3);
        bt_plus1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Plusbutton_3(Book_Activity.this).show();
            }
        });

        //数组初始化以及绑定MyGridViewAdapter
        mGridView = findViewById(R.id.gridView_1);
        mGridView.setOnItemLongClickListener(this);//长按事件监听
        myList = getMenuAdapter(pic, name);//初始化Array
        mAdapter = new MyGridViewAdapter(Book_Activity.this, myList);
        mGridView.setAdapter(mAdapter);

        //设置GridView中item为单击事件
        GridView gridView = findViewById(R.id.gridView_1);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(Book_Activity.this, TextEdit_Activity.class);
                startActivity(intent);

            }
        });
    }

    //GridView代码
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (isShowDelete) {
            isShowDelete = false;
        } else {
            isShowDelete = true;
            mAdapter.setIsShowDelete(isShowDelete);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    delete(position);//删除选中项
                    mAdapter = new MyGridViewAdapter(Book_Activity.this, myList);//重新绑定一次adapter
                    mGridView.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();//刷新gridview
                }

            });
        }

        mAdapter.setIsShowDelete(isShowDelete);//setIsShowDelete()方法用于传递isShowDelete值
        return true;
    }

    //删除函数：删除选中项方法
    private void delete(int position) {
        ArrayList<HashMap<String, Object>> newList = new ArrayList<HashMap<String, Object>>();
        if (isShowDelete) {
            myList.remove(position);
            isShowDelete = false;
        }
        newList.addAll(myList);
        myList.clear();
        myList.addAll(newList);

        //重新将items设置为单击事件
        GridView gridView = findViewById(R.id.gridView_1);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(Book_Activity.this, TextEdit_Activity.class);
                startActivity(intent);
            }
        });
    }

    //填充函数：将数据装入List
    private ArrayList<HashMap<String, Object>> getMenuAdapter(
                                                              int[] menuImageArray, String[] menuNameArray) {
        ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < menuImageArray.length; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("image", menuImageArray[i]);
            map.put("name",menuNameArray[i]);
            data.add(map);
        }
        return data;
    }
    //菜单功能代码
    //重写onCreateOptionMenu()方法来添加一个选项菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = new MenuInflater(this);
        menuInflater.inflate(R.menu.menu_book,menu);
        return super.onCreateOptionsMenu(menu);
    }

    //重写onOptionsItemSelected()方法，指定各个菜单被选择时，所以做的处理
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.Edit_book:
                break;

            case R.id.Time_book:
                break;

            case R.id.Updata_book:
                break;

            case R.id.Name_book:
                break;
            default:break;
        }
        return super.onOptionsItemSelected(item);
    }

    //显示加(+)号行为函数：显示新建图片组和文本组代码
    public AlertDialog.Builder Plusbutton_3(final Context context) {
        LinearLayout loginDialog= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.plusbutton_new,null);
        AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(loginDialog);
        return builder;
    }
}